﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MixScriptWeb.control;
using MixScriptWeb.Share;
using MixScriptWeb.AppCode.gjips;


namespace MixScriptWeb.Jideco.gjips
{
    public partial class fUnitMaterial : cCommonWeb, IMIxCommon
    {
        private static DataTable dt;
        private static SortDirection sd;
        private static cRefGjips oReff;
        // private static Control[] ctl;
        private const string stblname = "vw_joid_unit_material_v";
        
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    addFindClickAttribute(this, new MixScriptTB[] { txtPartno}, new string[] { "SRCMATGJ" });
                    //txtItNo.ReadOnly = true;
                    //  setSortExpression(gvdetail, new string[] { "banknum", "bankname", "address", "city", "country", "ac" });
                    
                    txtPartno.Tbx.Attributes.Add("readonly", "readonly");
                    string[] sheadertext = new string[] { "Measurement Code","Measurement Name"};
                    string[] sdatavalue = new string[] {"unms","unmsns"};
                    initGvColumn(gvdetail, sheadertext, sdatavalue);
                }
                mn = (MainMenu)this.Master;
                hideStatusBar();
                // oReff = new cReferences();
                // if(gvdetail.Rows.Count>0)


            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message, msgType.msgError);
            }


        }

        protected void gvdetail_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {

                //oUM = new cUserManagement();

                if (gvdetail.SelectedIndex > -1)
                {
                    txtPartno.Text = gvdetail.SelectedRow.Cells[0].Text;
                    //txtwrhcode.Text = gvdetail.SelectedRow.Cells[2].Text;
                    //txtWrhName.Text = gvdetail.SelectedRow.Cells[3].Text;
                    //txtWrhDesc.Text = gvdetail.SelectedRow.Cells[4].Text;
                    
                    
                    

                    //ini ngehapus &nbsp;
                    removeNbsp(new MixScriptTB[] { txtPartno});
                    

                }
            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }

        protected void gvdetail_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (sd == SortDirection.Ascending)
            {
                sd = SortDirection.Descending;
            }
            else
            {
                sd = SortDirection.Ascending;
            }

            DataView dataView = new DataView(dt);
            dataView.Sort = e.SortExpression + " " + ShortDirection(sd);

            bindGridview(gvdetail, dataView);
        }

        protected void gvdetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

            gvdetail.PageIndex = e.NewPageIndex;
            //gvdetail.DataBind();
            bindGridview(gvdetail, dt);


        }

        protected void btnFind_Click(object sender, EventArgs e)
        {
            try
            {
                oReff = new cRefGjips(); //new cUserManagement();
                dt = new DataTable();
                Control[] ctl = new Control[] { txtPartno, txtPartName};
                if (oReff.getInquiry(ref dt, genInqParam(ctl), stblname, out errMsg))
                {

                }
                else
                {
                    ShowStatusBar("Record Not Found", msgType.msgError);
                }
                bindGridview(gvdetail, dt);



            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message, msgType.msgError);
            }

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                //oReff = new cReferences();
                //string ss = "";
                //if (isvalid())
                //{
                //    if (txtwrhid.Text.Trim() == string.Empty)
                //    {

                //        if (oReff.AddNewRowDB(out errMsg, "tb_wrhmst", new int[] { 0 }, new string[] { "WRHID" }, false, txtwrhid.Text, txtwrhcode.Text, txtWrhName.Text, txtWrhDesc.Text))
                //        {
                //            ShowStatusBar(errMsg, msgType.msgSuccess);
                //            txtwrhid.Text = ss;
                //            btnFind_Click(btnFind, new EventArgs());
                //            btnClear_Click(btnClear, new EventArgs());
                //        }
                //        else
                //        {
                //            ShowStatusBar(errMsg, msgType.msgError);
                //        }

                //    }
                //    else
                //    {
                //        if (oReff.isRecordExist("tb_wrhmst", new string[] { "wrhid" }, new object[] { txtwrhid.Text}))
                //        {
                //            if (oReff.EditRowDtDB(out errMsg, "tb_wrhmst", new string[] { "wrhid" }, new object[] { txtwrhid.Text }, false, txtwrhid.Text, txtwrhcode.Text, txtWrhName.Text, txtWrhDesc.Text))
                //            {
                //                ShowStatusBar(errMsg, msgType.msgSuccess);
                //                btnFind_Click(btnFind, new EventArgs());
                //                btnClear_Click(btnClear, new EventArgs());
                //            }
                //            else
                //            {
                //                ShowStatusBar(errMsg, msgType.msgError);
                //            }

                //        }
                //        else
                //        {
                //            ShowStatusBar("Record Not Found");
                //        }
                //    }
                //}
                //else
                //{
                //    // ShowStatusBar(errMsg);
                //}
            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }



        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                ////cek apakah user sudah pilih data yg akan dihapus
                //if (txtwrhid.Text != string.Empty)
                //{
                //    //cek apakah data itu ada
                //    if (oReff.isRecordExist("tb_wrhmst", new string[] { "wrhid" }, new object[] { txtwrhid.Text }))
                //    {
                //        oReff = new cReferences();

                //        //coba hapus
                //        //                                nama primary key                nilainya          nama tablenya   pesan error
                //        if (oReff.deleteRowDB(new string[] { "wrhid" }, new object[] { txtwrhid.Text }, "tb_wrhmst", out errMsg))
                //        {
                //            //jika berhasil
                //            ShowStatusBar(em.getError(8), msgType.msgSuccess);
                //        }
                //        else
                //        {   //jika gagal
                //            ShowStatusBar(errMsg);
                //        }

                //    }
                //    else
                //    {   //jika data tidak ditemukan
                //        ShowStatusBar(em.getError(7));
                //    }
                //}
                //else
                //{
                //    ShowStatusBar(em.getError(7));
                //}
            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            try
            {
                errMsg = "";
               setValid(new MixScriptTB[] { txtPartno, txtPartName });
               clearMixTB(new MixScriptTB[] { txtPartno, txtPartName });
                

               
            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }

        }



        #region IMIxCommon Members

        //validasi inputan
        public bool isvalid()
        {
            try
            {
                //errMsg = "";
                ////set semua control valid
                //setValid(new MixScriptTB[] { txtwrhid, txtwrhcode, txtWrhName, txtWrhDesc });
                ////cek semua field mandatory
                //if (MustFilledOk(new Control[] {  txtwrhcode, txtWrhName }, out errMsg))
                //{
                //    if (txtwrhcode.Text.Trim().Length == 3)
                //    {

                //    }
                //    else
                //    {
                //        ShowStatusBar(errMsg + " " + em.getError(15));
                //        return false;
                        
                //    }

                //}
                //else
                //{
                //    ShowStatusBar(errMsg + " " + em.getError(2));
                //    return false;
                //}
                return true;
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }

        #endregion




        #region Button





        #endregion


        #region Gridview
        #region Gridview Event





        protected void gvdetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.DataItemIndex == -1)
                    return;
                e.Row.Attributes.Add("onMouseOver", "this.style.cursor='pointer';");
                e.Row.Attributes.Add("onclick", this.GetPostBackClientEvent(gvdetail, "Select$" + e.Row.RowIndex.ToString()));
                //e.Row.Attributes.Add("onclick", this.GetPostBackEventReference(gvdetail, "Select$" + e.Row.RowIndex.ToString())); //  (gvdetail, "Select$" + e.Row.RowIndex.ToString()));


            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }



        #endregion

      

 

        protected void btnReport_Click(object sender, EventArgs e)
        {
            try
            {

                if (dt.Rows != null)
                {
                    Session["datareport"] = dt;
                    Response.Redirect("~/tesreport.html");
                }
            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }
        //protected void cbAll_CheckedChanged(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        bool b = ((CheckBox)sender).Checked;


        //        foreach (GridViewRow gr in gvdetail.Rows)
        //        {
        //            CheckBox cb = (CheckBox)gr.Cells[0].FindControl("cbitem");
        //            cb.Checked = b;
        //        }

        //    }
        //    catch (Exception ex)
        //    {

        //        ShowStatusBar(ex.Message);
        //    }
        //}

        //protected void lbDelete_Click(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        oReff = new cReferences();
        //        bool isok = true;
        //        int i = 0;
        //        foreach (GridViewRow gr in gvdetail.Rows)
        //        {

        //            if (((CheckBox)gr.Cells[0].FindControl("cbitem")).Checked)
        //            {
        //                string sid = gr.Cells[1].Text;

        //                if (!oReff.deleteRowDB(new string[] { "wrhid" }, new string[] { sid }, "tb_wrhmst", out errMsg))
        //                {
        //                    ShowStatusBar(em.getError(9) + sid + " " + errMsg);
        //                    isok = false;
        //                    continue;
        //                }
        //                else
        //                {
        //                    i++;
        //                }
        //            }

        //        }

        //        if (i == 0 && isok)
        //        {
        //            ShowStatusBar("Please Select Item First");
        //        }
        //        else if (isok)
        //        {
        //            btnClear_Click(this, new EventArgs());
        //            btnFind_Click(this, new EventArgs());
        //            ShowStatusBar("Delete Completed", msgType.msgSuccess);
        //        }
        //    }
        //    catch (Exception ex)
        //    {

        //        ShowStatusBar(ex.Message);
        //    }
        //}

        #endregion
    }
}